MODULE menu_save
  !-----------------------------------------------------------
  ! KRUUNUNHAKA basis set tool kit - basis set saving menu
  !-----------------------------------------------------------
  ! See CHANGELOG
  !-----------------------------------------------------------
  ! Copyright (C) 2003-2008 Pekka Manninen, 2010 Jussi Lehtola
  !
  ! This program is distributed under the terms of the GNU General
  ! Public License as published by the Free Software Foundation;
  ! either version 2 of the License, or (at your option) any later version.
  !
  ! This program is distributed in the hope that it will be useful,
  ! but WITHOUT ANY WARRANTY; without even the implied warranty of
  ! MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
  !
  ! Please note that this program as well as its 
  ! author must be properly cited whenever the program or some parts 
  ! originated on it are employed. 
  !
  !-----------------------------------------------------------
  USE definitions
  USE textutil
  USE inout
  USE menu
  USE sorting

  IMPLICIT NONE

CONTAINS
!-----------------------------------------------------------
  SUBROUTINE save_menu(X_l,C_l,nbas)
    ! Basis set save menu
    IMPLICIT NONE
    REAL(KIND=prec), DIMENSION(:,:), ALLOCATABLE, INTENT(IN) :: X_l
    REAL(KIND=prec), DIMENSION(:,:,:), ALLOCATABLE, INTENT(IN) :: C_l
    INTEGER, DIMENSION(MAXL,2), INTENT(IN) :: nbas    
    CHARACTER(LEN=TERMWIDTH) :: savemenu_tit
    CHARACTER(LEN=TERMWIDTH), DIMENSION(5) :: savemenu
    CHARACTER(LEN=5) :: savesel

    ! File name to save as
    CHARACTER(LEN=TERMWIDTH), SAVE :: filename = ' '

    INTEGER :: sel
    LOGICAL :: ok
    LOGICAL :: exist

    savemenu(1)='Set file name'
    savemenu(2)='Save basis set in Dalton format'
    savemenu(3)='Save basis set in Gaussian''94 format'
    savemenu(4)='Save basis set in PSI3 format'
    savemenu(5)='Return to main menu'
    savesel="1234r"

    ok=.FALSE.
    DO WHILE(.NOT. ok)
       IF(LEN(TRIM(filename))>0) THEN
          WRITE(savemenu_tit,'(A,A)') "Save menu, saving basis set to ", &
               TRIM(filename)
       ELSE
          savemenu_tit="Save menu"
       END IF
       
       ! Print out menu
       sel=make_selection(savemenu_tit,savemenu,savesel,'*')

       SELECT CASE(sel)
       CASE(1)
          ! Set input file name
          WRITE (*,'(A)',advance='no') 'Give output file name: '
          READ (*,*) filename
          WRITE (*,*) ''
       CASE(2)
          ! Save basis in Dalton format
          IF(LEN(TRIM(filename))==0) THEN
             WRITE (*,'(A)') 'Set file name first!'
          ELSE
             ! Check if file already exists
             INQUIRE(FILE=filename, EXIST=exist)
             IF(exist) THEN
                ! File exists, overwrite?
                IF(make_yn('Overwrite existing file?')) THEN
                   CALL write_dalton_basis(filename,nbas,X_l,C_l)
                   ok=.TRUE.
                ENDIF
             ELSE
                ! File does not exist, write output.
                CALL write_dalton_basis(filename,nbas,X_l,C_l)
                ok=.TRUE.
             ENDIF
          END IF
       CASE(3)
          ! Save basis in Gaussian'94 format
          IF(LEN(TRIM(filename))==0) THEN
             WRITE (*,'(A)') 'Set file name first!'
          ELSE
             ! Check if file already exists
             INQUIRE(FILE=filename, EXIST=exist)
             IF(exist) THEN
                ! File exists, overwrite?
                IF(make_yn('Overwrite existing file?')) THEN
                   CALL write_gaussian_basis(filename,X_l,C_l)
                   ok=.TRUE.
                ENDIF
             ELSE
                ! File does not exist, write output.
                CALL write_gaussian_basis(filename,X_l,C_l)
                ok=.TRUE.
             ENDIF
          END IF
       CASE(4)
          ! Save basis in PSI3 format
          IF(LEN(TRIM(filename))==0) THEN
             WRITE (*,'(A)') 'Set file name first!'
          ELSE
             ! Check if file already exists
             INQUIRE(FILE=filename, EXIST=exist)
             IF(exist) THEN
                ! File exists, overwrite?
                IF(make_yn('Overwrite existing file?')) THEN
                   CALL write_psi_basis(filename,X_l,C_l)
                   ok=.TRUE.
                ENDIF
             ELSE
                ! File does not exist, write output.
                CALL write_psi_basis(filename,X_l,C_l)
                ok=.TRUE.
             ENDIF
          END IF

          IF(LEN(TRIM(filename))==0) THEN
             WRITE (*,'(A)') 'Set file name first!'
          ELSE
             ok=.TRUE.
          END IF
       CASE(5)
          ! Return to main menu
          ok=.TRUE.
       END SELECT
    END DO
  END SUBROUTINE save_menu
!-----------------------------------------------------------
END MODULE MENU_SAVE
